package com.ehome.user.Activity;

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.ehome.user.Fragment.FoundFragment;
import com.ehome.user.Fragment.HomeFragment;
import com.ehome.user.Fragment.PersonFragment;
import com.ehome.user.Module.TabModule;
import com.ehome.user.Module.user;
import com.ehome.user.R;



import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.jpush.android.api.JPushInterface;


public class MainActivity extends AppCompatActivity {

    private FragmentTabHost mTabHost;
    private List<TabModule> tablist = new ArrayList<>(3);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bmob.initialize(this, "fdc47c71f6bbd2d352fc3f96da5a83e0");
        initTab();
    }

    private void initTab() {
        TabModule tab1 = new TabModule(R.drawable.tab_home_selector, "home", "主页", HomeFragment.class);
        TabModule tab2 = new TabModule(R.drawable.tab_found_selector, "found", "发现", FoundFragment.class);
        TabModule tab3 = new TabModule(R.drawable.tab_person_selector, "person", "我的", PersonFragment.class);

        tablist.add(tab1);
        tablist.add(tab2);
        tablist.add(tab3);

        mTabHost=(FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        for (TabModule tab : tablist) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(tab.getTag());
            tabSpec.setIndicator(buildView(tab));
            mTabHost.addTab(tabSpec, tab.getmFragment(), null);
        }
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mTabHost.setCurrentTabByTag(tab1.getTag());
    }

    private View buildView(TabModule tab) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
        ImageView tabImg = (ImageView) view.findViewById(R.id.tab_img);
        TextView text = (TextView) view.findViewById(R.id.tab_text);
        tabImg.setImageResource(tab.getIcon());
        text.setText(tab.getText());
        return view;
    }

    @Override
    protected void onStart() {
        super.onStart();
//        BmobQuery<user> query = new BmobQuery<user>();
//        query.addWhereEqualTo("phone","15903673470");
//        query.findObjects(new FindListener<user>() {
//            @Override
//            public void done(List<user> list, BmobException e) {
//                if(e==null){
//                    //Toast.makeText(MainActivity.this,list.get(0).getPhone(),Toast.LENGTH_SHORT).show();
//                }else {
//                    Toast.makeText(MainActivity.this,"失败",Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
    }
}
